package net.dgg.dggcloud.service.jrcrm.service.cms;

import net.dgg.dggcloud.service.common.base.service.BaseService;

import net.dgg.dggcloud.service.jrcrm.dao.cms.RecordDao;
import net.dgg.dggcloud.service.jrcrm.utils.BizExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @ClassName: RecordService
 * @Description:
 * @author：guowei
 * @date： 2017/11/6
 */

@Service
public class RecordService extends BaseService {

    @Autowired
    private RecordDao recordDao;

    /**
     * 根据客户下面所有电话号码，查询录音 TODO 待实现时间段，外呼号的条件查询
     *  contactsNumber 电话
     *  month 分表月份，格式“201711”
     *  startTime 开始时间
     *  endTime 结束时间
     *  src 外呼号
     * @param map
     * @return
     */
    public List<Map> queryRecordByParams(Map map) {
        BizExceptionUtil.isException(null==map.get("month"),"请传入通话记录月份");
        try {
            Integer month = Integer.valueOf(map.get("month").toString());
            map.put("callMonth",month);
        }catch (Exception e){
            BizExceptionUtil.isException(true,"通话记录月份格式错误");
        }
        String contactsNumber = map.get("contactsNumber") == null ? "" : map.get("contactsNumber").toString();
//        String[] contactsNumbers = contactsNumber.split(",");
//        String numbers = "";
//        for (int i = 0; i < contactsNumbers.length; i++) {
//            if(numbers.equals("")){
//                numbers = numbers + "'"+contactsNumbers[i]+"','0"+ contactsNumbers[i]+"'";
//            }else{
//                numbers = numbers + ",'"+contactsNumbers[i]+"','0"+ contactsNumbers[i]+"'";
//            }
//        }
        map.put("contactsNumbers",contactsNumber);
        return recordDao.queryRecordByParams(map);
    }

    /**
     * 根据客户下面所有电话号码，查询录音 数量
     *
     * @param map
     * @return
     */
    public Integer countRecordByParams(Map map) {
        String contactsNumber = map.get("contactsNumber").toString();
        String[] contactsNumbers = contactsNumber.split(",");
        String numbers = "";
        for (int i = 0; i < contactsNumbers.length; i++) {
            if(numbers.equals("")){
                numbers = numbers + "'"+contactsNumbers[i]+"','0"+ contactsNumbers[i]+"'";
            }else{
                numbers = numbers + ",'"+contactsNumbers[i]+"','0"+ contactsNumbers[i]+"'";
            }
        }
        map.put("contactsNumbers",contactsNumbers);
        return recordDao.countRecordByParams(map);
    }
}
